package com.example.demo.controller;

import com.example.demo.pojo.ColumnArticle;
import com.example.demo.service.ColumnArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by hasee
 * on 2019/10/29 10:52
 */
@CrossOrigin
@Controller
@RequestMapping("/column")
public class ColumnArticleController {

    @Autowired
    private ColumnArticleService columnArticleService;

    /**
     * 查询专栏列表
     * **/
    @RequestMapping(value = "/selectColumnList")
    @ResponseBody
    public List<ColumnArticle> selectColumnList(@RequestBody int start){

        List<ColumnArticle> msgFromService=new ArrayList<>();
        int result = 0;
        System.out.println("得到的请求值："+start);
        msgFromService = columnArticleService.selectColumnList(start);
        try {
            if(msgFromService != null){
                System.out.println("向前端返回结果："+msgFromService);
            }else{
                System.out.println("未查询到结果:");
                msgFromService = null;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("controller中selectColumnList报空指针错误");
            msgFromService = null;
        }
     return msgFromService;
    }


    /**查询持久化的专栏文章**/
    @RequestMapping(value = "/selectColumnInfo")
    @ResponseBody
    public ColumnArticle selectColumnInfo(@RequestBody ColumnArticle columnArticle){
        ColumnArticle resultFromService = new ColumnArticle();
        String result = "获取失败";
        System.out.println("得到的请求值："+columnArticle);
        try {
            resultFromService = columnArticleService.selectColumnInfo(columnArticle);

            if(resultFromService != null){
                result = "获取成功";
            }else{
                result = "获取失败";
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("controller中selectColumnInfo报空指针错误");
            resultFromService = null;
        }
        return resultFromService;
    }

    /**持久化专栏文章**/
    @RequestMapping(value="/uploadColumn")
    @ResponseBody
    public int saveUploadColumnInfo(@RequestParam("userName") String userName,@RequestParam("articleTitle") String articleTitle,
                                    @RequestParam("articleContent") String articleContent,@RequestParam("articleCategory") String articleCategory,
                                    @RequestParam("tag1Name") String tag1Name,
                                    @RequestParam("tag2Name") String tag2Name,@RequestParam("tag3Name") String tag3Name,
                                    @RequestParam("tag4Name") String tag4Name, @RequestParam("tag5Name") String tag5Name,
                                    @RequestParam(value = "columnCover", required = false)
                                                List<MultipartFile> columnCover, HttpServletRequest request) {
        //resource类对象
        ColumnArticle columnArticleInfo = new ColumnArticle();
        columnArticleInfo.setUserName(userName);
        columnArticleInfo.setArticleTitle(articleTitle);
        columnArticleInfo.setArticleContent(articleContent);
        columnArticleInfo.setArticleCategory(articleCategory);
        columnArticleInfo.setTag1Name(tag1Name);
        columnArticleInfo.setTag2Name(tag2Name);
        columnArticleInfo.setTag3Name(tag3Name);
        columnArticleInfo.setTag4Name(tag4Name);
        columnArticleInfo.setTag5Name(tag5Name);
        System.out.println(columnArticleInfo.toString());
        //上传文件附带的封面图
        if(!columnCover.isEmpty() && columnCover.size()>0) {// && fileCover.isEmpty() && fileCover.size()>0
            System.out.println(columnCover);
            //UUID随机文件名
            UUID randomUUIDname = UUID.randomUUID();
            for (MultipartFile files : columnCover) {
                List<String> list = new ArrayList<String>();
                String originalFilename = files.getOriginalFilename();
                //封面图存储地址
                /*
                * dirPath的方式会导致每次存储位置都存储在C:\Users\hasee\AppData\Local\Temp，
                * 即spring boot内置tomcat的位置
                * */
//                String dirPath = request.getSession().getServletContext().getRealPath("\\uploadcolumncover\\");
                //专栏封面图存储位置
                /*
                * 以下方式直接将得到的上传文件指定到相应位置
                * */
                String path = "D:\\apache-tomcat-7.0.85\\webapps\\uploadfilecover\\uploadcolumncover";
                //存储文件的后缀
                String fileCoverType = ".jpg";
//                File filePath = new File(dirPath);
                File filePath = new File(path);
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
//				String localUrl="\\http:\\\\localhost:8080/uploadfilecover\\";localUrl+
                String newFilename = "_" + randomUUIDname + fileCoverType;//+"_"+originalFilename  "Cover"+
//                System.out.println("封面图存放在:" + dirPath + newFilename);
                System.out.println("封面图存放在:" + path + newFilename);

                try {
//                    files.transferTo(new File(dirPath + "\\" + newFilename));
                    files.transferTo(new File(path + "\\" + newFilename));
                    columnArticleInfo.setArticleCover(newFilename);//dirPath+"\\"+

                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                    return 0;
                }
            }
            columnArticleService.saveUploadColumnInfo(columnArticleInfo);
        }
        return 1;
    }

    /**
     * 查询所有专栏，无select条件/get请求
     * **/
    @RequestMapping(value = "/getArticleList")
    @ResponseBody
    public List<ColumnArticle> allArticleList(){
        List<ColumnArticle> articleList = null;
        try {
            articleList = columnArticleService.allArticleList();
            if(articleList == null){
                return articleList;
            }else{
                return articleList;
            }
        }catch (Exception e){
            articleList = null;
        }
        return articleList;
    }

}
